﻿@using Adventureworks.Domain;
@model IEnumerable<ShoppingCartItem>
@{
    ViewBag.Title = "Index";
}
<script type="text/javascript">
    $(function () {
        // Document.ready -> link up remove event handler
        $(".RemoveLink").click(function () {
            // Get the id from the link
            var recordToDelete = $(this).attr("data-id");

            if (recordToDelete != '') {

                // Perform the ajax post
                $.post("/ShoppingCart/RemoveFromCart", { "id": recordToDelete },
                    function (data) {
                        // Successful requests get here
                        // Update the page elements
                        if (data.ItemCount == 0) {
                            $('#row-' + data.DeleteId).fadeOut('slow');
                        } else {
                            $('#item-count-' + data.DeleteId).text(data.ItemCount);
                        }

                        $('#cart-total').text(data.CartTotal);
                        $('#update-message').text(data.Message);
                        $('#cart-status').text('Cart (' + data.CartCount + ')');
                    });
            }
        });
    });
</script>
<h3>
    <em>Review</em> your cart:
</h3>
<p class="button">
    @Html.ActionLink("Checkout >>", "AddressAndPayment", "Checkout")
</p>
<div id="update-message">
</div>
<table class="cart">
    <tr>
        <th>
            Product Name
        </th>
        <th>
            Price (each)
        </th>
        <th>
            Quantity
        </th>
        <th>
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr id="row-@item.ShoppingCartItemID">
            <td>
                @Html.ActionLink(@item.Product.Name, "Details", "Store", new
           {
               id = @item.ProductID
           }, null)
            </td>
            <td>
                @item.Product.ListPrice
            </td>
            <td>
                @item.Quantity
            </td>
            <td>
                <a href="#" class="RemoveLink" data-id="@item.ShoppingCartItemID">Remove from cart</a>
            </td>
        </tr>
    }
    <tr>
        <td>
            Total
        </td>
        <td>
        </td>
        <td>
        </td>
        <td id="cart-total">
            @ViewBag.CartTotal
        </td>
    </tr>
</table>
